using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using NTTHAO.CommonWeb;
using NTTHAO.Web.BL.Category;

public partial class admin_webparts_Plug_ins_Category_CategoryInput : System.Web.UI.UserControl
{
    public Category _Cat = null;

    protected void Page_Load(object sender, EventArgs e)
    {
        btnSave.Value = btnSave2.Value = LanguageUtility.GetLanguageText("SAVE", Session);
        chkActive.Text = LanguageUtility.GetLanguageText("ACTIVE", Session);

        try
        {
            cmbSelectType.Items.Clear();
            cmbSelectType.Items.Add(new ListItem(SelectType.SingleSelect.ToString(), ((short)SelectType.SingleSelect).ToString()));
            cmbSelectType.Items.Add(new ListItem(SelectType.MultiSelect.ToString(), ((short)SelectType.MultiSelect).ToString()));

            cmbValueType.Items.Clear();
            cmbValueType.Items.Add(new ListItem(NTTHAO.Web.BL.Category.ValueType.Text.ToString(), ((short)NTTHAO.Web.BL.Category.ValueType.Text).ToString()));
            cmbValueType.Items.Add(new ListItem(NTTHAO.Web.BL.Category.ValueType.Integer.ToString(), ((short)NTTHAO.Web.BL.Category.ValueType.Integer).ToString()));
            cmbValueType.Items.Add(new ListItem(NTTHAO.Web.BL.Category.ValueType.Decimal.ToString(), ((short)NTTHAO.Web.BL.Category.ValueType.Decimal).ToString()));
            cmbValueType.Items.Add(new ListItem(NTTHAO.Web.BL.Category.ValueType.NaturalNumber.ToString(), ((short)NTTHAO.Web.BL.Category.ValueType.NaturalNumber).ToString()));
            cmbValueType.Items.Add(new ListItem(NTTHAO.Web.BL.Category.ValueType.PositiveInteger.ToString(), ((short)NTTHAO.Web.BL.Category.ValueType.PositiveInteger).ToString()));
            cmbValueType.Items.Add(new ListItem(NTTHAO.Web.BL.Category.ValueType.PositiveDecimal.ToString(), ((short)NTTHAO.Web.BL.Category.ValueType.PositiveDecimal).ToString()));
            cmbValueType.Items.Add(new ListItem(NTTHAO.Web.BL.Category.ValueType.NegativeInteger.ToString(), ((short)NTTHAO.Web.BL.Category.ValueType.NegativeInteger).ToString()));
            cmbValueType.Items.Add(new ListItem(NTTHAO.Web.BL.Category.ValueType.NegativeDecimal.ToString(), ((short)NTTHAO.Web.BL.Category.ValueType.NegativeDecimal).ToString()));

            if (!IsPostBack && Request.QueryString["id"] != null)
            {
                int catID = Convert.ToInt32(Request.QueryString["id"]);
                _Cat = new Category(catID);
                if (_Cat.ID < 1)
                {
                    throw new Exception();
                }

                txtName.Text = _Cat.Name;
                cmbValueType.Text = ((short)_Cat.ValueType).ToString();
                cmbSelectType.Text = ((short)_Cat.SelectType).ToString();
                chkActive.Checked = _Cat.Actived;
            }
        }
        catch
        {
            string redirectURL = "?type=" + Request.QueryString["type"] + "&name=" + Request.QueryString["name"];
            Response.Redirect(redirectURL, false);
            return;
        }
    }
    public void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            Category cat = null;
            if (Request.QueryString["id"] != null)
            {
                int catID = Convert.ToInt32(Request.QueryString["id"]);
                cat = new Category(catID);
            }
            else
            {
                cat = new Category();
                cat.SiteID = WebGlobal.GetSite().ID;
                cat.LanguageID = LanguageUtility.GetLanguageID();
            }

            cat.Name = txtName.Text.Trim();
            cat.SelectType = (SelectType)Convert.ToInt16(cmbSelectType.SelectedValue);
            cat.ValueType = (NTTHAO.Web.BL.Category.ValueType)Convert.ToInt16(cmbValueType.SelectedValue);
            cat.Actived = chkActive.Checked;

            if (Request.QueryString["id"] != null)
            {
                cat.Update();
            }
            else
            {
                cat.Insert();
            }
        }
        catch { }

        string redirectURL = "?type=" + Request.QueryString["type"] + "&name=" + Request.QueryString["name"];
        Response.Redirect(redirectURL, false);
        return;
    }
}
